package com.epam.iunna.bookshop.controller.actions;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.iunna.bookshop.controller.application.Constants;
import com.epam.iunna.bookshop.controller.utils.dispatch.ActionInt;
import com.epam.iunna.bookshop.model.User;
import com.epam.iunna.bookshop.model.UserDao;

public class CreateUser implements ActionInt{

    private static final String EMAIL_PATTERN = 
            "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    public static final String DEFAULT_USER_ROLE="user";
    Pattern emailPattern;
    public CreateUser(){
        emailPattern = Pattern.compile(EMAIL_PATTERN);
    }
    @Override
    public String perform(HttpServletRequest request,
            HttpServletResponse response) {
        String login = request.getParameter("login");
        String password = request.getParameter("password");
        String passwordConfirm = request.getParameter("passwordConfirm");
        String name = request.getParameter("name");
        String surname = request.getParameter("surname");
        String tel = request.getParameter("tel");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        String money = request.getParameter("money");
        
        Map<String,String> errorMap = new HashMap<String,String>();
        
        //validation
        if( login.isEmpty() ){
            errorMap.put("login", " * required");
        }
        if( password.isEmpty() ){
            errorMap.put("password", " * required");
        }
        if( passwordConfirm.isEmpty() ){
            errorMap.put("passwordConfirm", " * required");
        }
        if( !password.equals(passwordConfirm) ){
            errorMap.put("passwordConfirm", " * confirmation password must match field password");
        }
        if( name.isEmpty() ){
            errorMap.put("name", " * required");
        }
        if( surname.isEmpty() ){
            errorMap.put("surname", " * required");
        }
        Matcher matcher = emailPattern.matcher(email);
        if( !matcher.matches() ){
            errorMap.put("email", " * must be a valid email address");
        }
        if( email.isEmpty() ){
            errorMap.put("email", " * required");
        }
        BigDecimal moneyBigDec = null;
        try{
            moneyBigDec = new BigDecimal(money);
        }catch(Exception e){
            errorMap.put("money", " not a number");
        }
        if( money.isEmpty() ){
            errorMap.put("moneyObj", " * required");
        }
        
        User user = new User();
        user.setAddress(address);
        user.setEmail(email);
        user.setLogin(login);
        user.setName(name);
        //save hash
        user.setPass(password);
        user.setRole(DEFAULT_USER_ROLE);
        user.setBanned(false);
        user.setSurame(surname);
        user.setTel(tel);
        user.setMoney(moneyBigDec);
        
        if( !errorMap.isEmpty() ){
            request.setAttribute("errorMap", errorMap);
            request.setAttribute("user", user);
            return "/createUser.jsp";
        }
        
        //call dao create user
        UserDao dao = new UserDao();
        try {
            dao.addUser(user);
        } catch (SQLException e) {
            //LOG
           e.printStackTrace();
            return Constants.FATAL_ERROR_PAGE_PATH;
        }
        
        return "/login.jsp";
    }

}
